﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNA_TD.Shells;

namespace XNA_TD
{
    public class ATTower : Tower
    {
        static ModelBone towerBone = null;
        static ModelBone gunBone;
        static LvlInfo startInfo;

        static Matrix towerTransform;
        static Matrix[] boneTransforms;
        static Matrix gunTransform;

        double gunRecoil;

        public ATTower(Vector3 pos)
            : base(pos, "Tower/ATTower")
        {
            if (towerBone == null)
            {
                towerBone = model.Bones["ATTower"];
                gunBone = model.Bones["ATTowerGun"];

                towerTransform = towerBone.Transform;
                gunTransform = gunBone.Transform;

                boneTransforms = new Matrix[model.Bones.Count];

                startInfo = new LvlInfo("", 5, 5, 20, 150, 300, GegnerTyp.Boden);

                startInfo
                    .Add("Damage Upgrade", 7, 5, 20, 150, 150, GegnerTyp.Boden)
                    .Add("Damage Upgrade", 15, 5, 20, 150, 450, GegnerTyp.Boden);

                startInfo
                    .Add("Speed Upgrade", 5, 5, 30, 150, 200, GegnerTyp.Boden)
                    .Add("Speed Upgrade", 5, 5, 50, 150, 300, GegnerTyp.Boden);


                var moreRange = startInfo
                    .Add("Range Upgrade", 5, 7, 20, 150, 200, GegnerTyp.Boden);
                moreRange
                    .Add("Damage Upgrade", 7, 7, 20, 150, 200, GegnerTyp.Boden)
                    .Add("Damage Upgrade", 15, 7, 20, 150, 450, GegnerTyp.Boden);
                moreRange
                    .Add("Speed Upgrade", 5, 7, 30, 150, 200, GegnerTyp.Boden)
                    .Add("Speed Upgrade", 5, 7, 50, 150, 300, GegnerTyp.Boden);
                moreRange
                    .Add("Range Upgrade", 5, 10, 20, 150, 200, GegnerTyp.Boden);
            }
            gunRecoil = 0;
            turretYawSpeed = 0.015f;

            lvlInfo = startInfo;
            ResetHealth();
        }

        public override void FixedGameUpdate()
        {
            base.FixedGameUpdate();
            gunRecoil = Math.Max(gunRecoil - 0.001f, 0);
        }

        protected override Matrix[] CalculateAbsoluteBoneTransforms()
        {
            towerBone.Transform = Matrix.CreateRotationZ(turretYaw) * towerTransform;

            gunBone.Transform = Matrix.CreateTranslation(0, (float)gunRecoil, 0) * gunTransform;

            model.Root.Transform = root * Matrix.CreateTranslation(position);
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);

            return boneTransforms;
        }

        protected override void Shot()
        {
            gunRecoil = 0.1;
            CalculateAbsoluteBoneTransforms();

            Vector3 translation = boneTransforms[gunBone.Index].Translation;
            Register(new ATTowerShell(translation, target, lvlInfo.attackDmg, 0.1f));
        }
    }
}
